/***************************************************************************
 创建者：华磊
 开始时间：         : 2017.02.13
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   码垛指令视窗
*                                                                         *
 ***************************************************************************/
#include "palletmotioninstructionview.h"
#include "ui_palletmotioninstructionview.h"
#include "palletinstructionview.h"
#include "../../programView/programview.h"

PalletMotionInstructionView::PalletMotionInstructionView(int styleId,QWidget *parent):
        QWidget(parent),
        ui(new Ui::PalletMotionInstructionView)
{
    ui->setupUi(this);

    initialDisplayStyle(styleId);

    //码垛号 1-16
    ui->palletIndex_lineEdit->setValidator(new QIntValidator(1, 16, this));
    //点数量 1-8
    ui->palletPathIndex_lineEdit->setValidator(new QIntValidator(1, 8, this));

//    //行列层不可编辑
//    ui->palletRow_lineEdit->setDisabled(true);
//    ui->palletColumn_lineEdit->setDisabled(true);
//    ui->palletLayer_lineEdit->setDisabled(true);
}

void PalletMotionInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);

        break;
    default:
        break;
    }
}

PalletMotionInstructionView::~PalletMotionInstructionView()
{
    delete ui;
}

void PalletMotionInstructionView::updateView()
{
    if(0 == palletInstructionView->instructionTypeFlag)
    {
        ui->J_radioButton->setChecked(true);
    }
    else if(1 == palletInstructionView->instructionTypeFlag)
    {
        ui->L_radioButton->setChecked(true);
    }

    ui->palletIndex_lineEdit->setText(QString::number(1));

    if(EM_PALLET_PATH_APPROACH == palletInstructionView->palletPathTypeFlag)
    {
        ui->palletPathType_label->setText("A_");
        ui->palletPathIndex_lineEdit->setText(QString::number(palletInstructionView->approachPointIndex));
        ui->palletPathIndex_lineEdit->setEnabled(true);
    }
    else if(EM_PALLET_PATH_FLEE == palletInstructionView->palletPathTypeFlag)
    {
        ui->palletPathType_label->setText("R_");
        ui->palletPathIndex_lineEdit->setText(QString::number(palletInstructionView->fleePointIndex));
        ui->palletPathIndex_lineEdit->setEnabled(true);
    }
    else
    {
        ui->palletPathType_label->setText("BTM");
        ui->palletPathIndex_lineEdit->clear();
        ui->palletPathIndex_lineEdit->setDisabled(true);
    }

    //行列层
    if(3 == palletInstructionView->rowColumnLayer.size() )
    {
        ui->palletRow_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[0]));
        ui->palletColumn_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[1]));
        ui->palletLayer_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[2]));
    }
    else
    {
        palletInstructionView->rowColumnLayer.resize(3);
        ui->palletRow_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[0]));
        ui->palletColumn_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[1]));
        ui->palletLayer_lineEdit->setText(QString::number(palletInstructionView->rowColumnLayer[2]));
    }

    ui->rate_lineEdit->setText(QString("%1").arg(palletInstructionView->instructionRate));
    ui->rateUnit_comboBox->setCurrentIndex(palletInstructionView->rateUnitTypeFlag);
    ui->rate_slider->setSliderPosition(palletInstructionView->instructionRate);

    ui->fixedType_comboBox->setCurrentIndex(palletInstructionView->fixedPositionTypeFlag);
    if(0 == palletInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_lineEdit->clear();
        ui->CNT_lineEdit->setEnabled(false);
        ui->CNT_slider->setEnabled(false);
    }
    else if(1 == palletInstructionView->fixedPositionTypeFlag)
    {
        ui->CNT_lineEdit->setEnabled(true);
        ui->CNT_slider->setEnabled(true);
        ui->CNT_lineEdit->setText(QString::number(palletInstructionView->cntValue));
        ui->CNT_slider->setSliderPosition(palletInstructionView->cntValue);
    }

    if(0 == palletInstructionView->selectSkipFlag)
    {
        ui->DI_checkBox->setChecked(false);
    }
    else if(1 == palletInstructionView->selectSkipFlag)
    {
        ui->DI_checkBox->setChecked(true);
        ui->DI_linEedit->setText(palletInstructionView->diIndex);
        ui->DISwitch_combox->setCurrentIndex(palletInstructionView->diSwitchFlag);
        ui->skipLblIndex_lineEdit->setText(palletInstructionView->skipLabelIndex);
    }

    ui->note_lineEdit->setText("");
}

void PalletMotionInstructionView::getInput()
{

    if(EM_PALLET_PATH_APPROACH == palletInstructionView->palletPathTypeFlag)
    {
        palletInstructionView->approachPointIndex = ui->palletPathIndex_lineEdit->text().toInt();
    }
    else if(EM_PALLET_PATH_FLEE == palletInstructionView->palletPathTypeFlag)
    {
        palletInstructionView->fleePointIndex = ui->palletPathIndex_lineEdit->text().toInt();
    }

    //
    palletInstructionView->instructionRate = ui->rate_lineEdit->text().toFloat();
    palletInstructionView->rateUnitTypeFlag = (FEED_RATE_FORMAT)ui->rateUnit_comboBox->currentIndex();
    palletInstructionView->rateUnit = ui->rateUnit_comboBox->currentText();
    palletInstructionView->fixedPositionTypeFlag = ui->fixedType_comboBox->currentIndex();
    palletInstructionView->cntValue = ui->CNT_lineEdit->text().toInt();

    palletInstructionView->diIndex = ui->DI_linEedit->text();
    palletInstructionView->diSwitchFlag = ui->DISwitch_combox->currentIndex();
    palletInstructionView->skipLabelIndex = ui->skipLblIndex_lineEdit->text();

}

void PalletMotionInstructionView::changeLanguage()
{
    ui->retranslateUi(this);
}

void PalletMotionInstructionView::on_J_radioButton_clicked()
{
    emit selectJInstruction();
}

void PalletMotionInstructionView::on_L_radioButton_clicked()
{
    emit selectLInstruction();
}

void PalletMotionInstructionView::on_moveToPoint_Btn_pressed()
{  

}

void PalletMotionInstructionView::on_moveToPoint_Btn_released()
{
    emit moveToPointFinish();
}

void PalletMotionInstructionView::on_rate_slider_valueChanged(int value)
{
    palletInstructionView->instructionRate = value;
    ui->rate_lineEdit->setText(QString::number(palletInstructionView->instructionRate));
}

void PalletMotionInstructionView::on_CNT_slider_valueChanged(int value)
{
    palletInstructionView->cntValue = value;
    ui->CNT_lineEdit->setText(QString::number(palletInstructionView->cntValue));
}

void PalletMotionInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void PalletMotionInstructionView::on_save_Btn_clicked()
{
    getInput();

    emit finishMotionInstruction();
    emit saveInstruction(palletInstructionView->palletMotionInstruction);
}

void PalletMotionInstructionView::on_DI_checkBox_clicked()
{
    emit selectSkipInstruction();
}

void PalletMotionInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit clearNote();
}

void PalletMotionInstructionView::on_fixedType_comboBox_activated(int index)
{
    palletInstructionView->fixedPositionTypeFlag = index;
    updateView();
}

void PalletMotionInstructionView::on_palletRow_lineEdit_textChanged(const QString &arg1)
{
    if(palletInstructionView->rowColumnLayer.size() == 3)
    {
        palletInstructionView->rowColumnLayer[0] = arg1.toInt();
    }
}

void PalletMotionInstructionView::on_palletColumn_lineEdit_textChanged(const QString &arg1)
{
    if(palletInstructionView->rowColumnLayer.size() == 3)
    {
        palletInstructionView->rowColumnLayer[1] = arg1.toInt();
    }
}

void PalletMotionInstructionView::on_palletLayer_lineEdit_textChanged(const QString &arg1)
{
    if(palletInstructionView->rowColumnLayer.size() == 3)
    {
        palletInstructionView->rowColumnLayer[2] = arg1.toInt();
    }
}
